﻿using QuanLyGiaoVuDAL.DTO;
using QuanLyGiaoVuDAL.Repositories.DatabaseRepositories;
using QuanLyGiaoVuService.CommonServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QuanLyGiaoVuService.CommandServices
{
    public class GiangVienCommandService: IGiangVienCommandService
    {
        private IGiangVienRepository _giangVienRepository;
        private ITaiLieuCommandService _taiLieuCommandService;
        private IThamSoService _thamSoService;

        public GiangVienCommandService(IGiangVienRepository giangVienRepository, ITaiLieuCommandService taiLieuCommandService, IThamSoService thamSoService)
        {
            _giangVienRepository = giangVienRepository;
            _taiLieuCommandService = taiLieuCommandService;
            _thamSoService = thamSoService;
        }

        public GiangVienDTO UpdateGiangVien(QuanLyGiaoVuDAL.DTO.GiangVienDTO giangVienDTO)
        {
            if (giangVienDTO.ID.Equals(Guid.Empty))
            {
                giangVienDTO.ID = Guid.NewGuid();
                var savedGiangVienDTO =_giangVienRepository.InsertGiangVien(giangVienDTO);

                //tao thu muc tai lieu cho giang vien
                ThuMucDTO thuMucMoi = new ThuMucDTO();
                thuMucMoi.MaThuMuc = savedGiangVienDTO.ID;
                thuMucMoi.TenThuMuc = savedGiangVienDTO.GetTenVaHocVi();
                Guid maThuMucCha;
                thuMucMoi.MaThuMucCha = Guid.TryParse(_thamSoService.GetThamSo("MaThuMucGocGiangVien"),out maThuMucCha) ? (Guid?)maThuMucCha : null;
                _taiLieuCommandService.TaoThuMucGiangVien(Guid.Parse(_thamSoService.GetThamSo("MaAdmin")), giangVienDTO.ID, thuMucMoi);
                return savedGiangVienDTO;
            }
            return _giangVienRepository.UpdateGiangVien(giangVienDTO);
        }

        public bool DeleteGiangVienByIds(String giangVienIds)
        {
            return _giangVienRepository.DeleteGiangVien(giangVienIds);
        }

        public QuanLyGiaoVuDAL.DTO.GiangVienDTO DeleteGiangVienByMaGiangVien(string maGiangVien)
        {
            throw new NotImplementedException();
        }

        public QuanLyGiaoVuDAL.DTO.GiangVienDTO InsertGiangVien(QuanLyGiaoVuDAL.DTO.GiangVienDTO giangVienDTO)
        {
            if (giangVienDTO.ID.Equals(Guid.Empty))
                giangVienDTO.ID = Guid.NewGuid();
            return _giangVienRepository.InsertGiangVien(giangVienDTO);
        }


        public bool InsertMonHoc(Guid giangVienId, Guid monHocId)
        {
            return _giangVienRepository.InsertMonHoc(giangVienId, monHocId);
        }

        public bool DeleteMonHoc(Guid giangVienId, Guid monHocId)
        {
            return _giangVienRepository.DeleteMonHoc(giangVienId, monHocId);
        }

        public bool InsertChucVu(Guid giangVienId, Guid chucVuId)
        {
            return _giangVienRepository.InsertChucVu(giangVienId, chucVuId);
        }

        public bool DeleteChucVu(Guid giangVienid, Guid chucVuId)
        {
            return _giangVienRepository.DeleteChucVu(giangVienid, chucVuId);
        }


        public LichRanhDTO InsertLichRanh(LichRanhDTO lichRanhDTO)
        {
            return _giangVienRepository.InsertLichRanh(lichRanhDTO);
        }

        public bool DeleteLichRanh(Guid maLichRanh)
        {
            return _giangVienRepository.DeleteLichRanh(maLichRanh);
        }

        public bool InsertLichRanhByList(Guid maGiangVien, string danhSachBuoi)
        {
            try
            {
                string[] buoiDK = danhSachBuoi.Split(',');
                var dsDaDk = _giangVienRepository.GetAllLichRanh(maGiangVien, null, null);
                foreach (var dk in dsDaDk)
                {
                    _giangVienRepository.DeleteLichRanh(dk.MaLichRanh);
                }
                if (!String.IsNullOrEmpty(danhSachBuoi))
                {
                    foreach (var buoi in buoiDK)
                    {
                        LichRanhDTO lichRanh = new LichRanhDTO();
                        lichRanh.MaLichRanh = Guid.NewGuid();
                        int thu;
                        bool buoiSang;
                        LayBuoiVaThu(Convert.ToInt32(buoi), out thu, out buoiSang);
                        lichRanh.Thu = thu;
                        lichRanh.Buoi = buoiSang;
                        lichRanh.GiangVien = maGiangVien;
                        _giangVienRepository.InsertLichRanh(lichRanh);
                    }
                }
                return true;
            }
            catch (Exception exception)
            {
                throw new Exception("Không thể lưu thông tin đăng ký giờ rãnh.");
            }
        }

        private void LayBuoiVaThu(int thuTu, out int thu, out bool buoi)
        {
            thu = (thuTu - 1)/ 2 + 2;
            buoi = (thuTu - 1) % 2 == 0; // true: sang;
        }


        public bool UpdateBoMon(Guid maGiangVien, int maBoMon)
        {
            var giangVien = _giangVienRepository.GetGiangVienById(maGiangVien);
            if (giangVien != null)
            {
                giangVien.MaBoMon = maBoMon;
                _giangVienRepository.UpdateGiangVien(giangVien);
                return true;
            }
            return false;
        }
    }
}
